﻿using MediatR;
using Microsoft.AspNetCore.Mvc;
using Production.ApplicationLib.Command;
using Production.ApplicationLib.Queries;
using Production.CommonLib.Constants;
using Production.CommonLib.Models;

namespace Production.WebApi.Controllers
{
    [ApiController]
    public class StandardBomController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly IStandardBomQueries _standardBomQueries;

        public StandardBomController(IMediator mediator,
            IStandardBomQueries standardBomQueries)
        {
            _mediator = mediator;
            _standardBomQueries = standardBomQueries;
        }

        [HttpGet(ApiRoutes.StandardBom.QueryStandardBoms)]
        public async Task<IActionResult> Get([FromQuery] StandardBomSearchRequestDto model)
        {
            if (model.Page > 0 || model.Limit > 0)
            {
                var data = await _standardBomQueries.QueryStandardBomsByPage(model);
                return Ok(data);
            }
            else
            {
                var data = await _standardBomQueries.QueryStandardBoms(model);
                return Ok(data);
            }
        }

        [HttpPost(ApiRoutes.StandardBom.CreateStandardBom)]
        public async Task<IActionResult> Post([FromBody] CreateStandardBomCommand command)
        {
            var result = await _mediator.Send(command);
            return Ok(result);
        }

        [HttpGet(ApiRoutes.StandardBom.GetStandardBom)]
        public async Task<IActionResult> Get(int id)
        {
            var result = await _standardBomQueries.GetStandardBom(id);
            return Ok(result);
        }
        
        [HttpPut(ApiRoutes.StandardBom.UpdateStandardBom)]
        public async Task<IActionResult> Put(int id, UpdateStandardBomCommand command)
        {
            command.Id = id;
            var result = await _mediator.Send(command);
            return Ok(result);
        }
    }
}